package com.awei.controller;


import com.awei.domain.Result;
import com.awei.domain.po.Article;
import com.awei.service.IArticleService;
import com.awei.utils.ThreadLocalUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author awei
 * @since 2024-11-15
 */
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private IArticleService articleService;

    /**
     * 添加文章
     * @param article
     * @return
     */
    @PostMapping
    public Result<String> addArticle(@RequestBody Article article) {
        Integer id = ThreadLocalUtil.get();
        article.setCreateUser(id);
        boolean isSaved = articleService.save(article);
        if (isSaved) {
            return Result.success("文章添加成功");
        } else {
            return Result.error("添加文章失败");
        }
    }

    /**
     * 文章详情
     * @param queryString
     * @return
     */
    @GetMapping("/detali")
    public Result<Article> articleDetails(Integer queryString){
        Article article = articleService.getById(queryString);
        return Result.success(article);
    }

    /**
     * 修改文章
     * @param article
     * @return
     */
    @PutMapping
    public Result updateArticle(@RequestBody Article article){
        boolean isSaved = articleService.updateById(article);
        if (isSaved) {
            return Result.success("文章更新成功！");
        } else {
            return Result.error("添加文章失败");
        }
    }

    /**
     * 删除文章
     * @param queryString
     * @return
     */
    @DeleteMapping
    public Result deleteArticle(Integer queryString){
        boolean isSaved = articleService.removeById(queryString);
        if (isSaved) {
            return Result.success();
        } else {
            return Result.error("添加文章失败");
        }
    }
    /**
     * 分页查询文章
     * @param pageNum
     * @param pageSize
     * @param status
     * @return
     */
    @GetMapping
    public Result queryArticleBypage(Integer pageNum,Integer pageSize,Integer status){
        Page<Article> page = new Page<>(pageNum,pageSize);
        Page<Article> page1 = articleService.page(page);
        return Result.success(page1);
    }
}
